
import React, { Component } from 'react';
import chukuList from '../../mobx/chukuList';
import {observer} from "mobx-react"
import {Table,Switch,Button} from "antd"
import moment from "moment"
import myuser from "../../mobx/myuser"
import { ajax } from '../../api';
import {EyeOutlined} from "@ant-design/icons"
import { history } from '../../utils';
@observer
class ChuList extends Component {

    state={
        columns:[
            {
                title:"序号",
                dataIndex:"index",
                key:"index",
                align:"center",
                render:(text,item,index)=>{
                    return index+1
                }
            },
            {
                title:"操作人员",
                align:"center",
                key:"username",
                dataIndex:"username",
            },
            {
                title:"配件类型",
                align:"center",
                render:(text,item,index)=>{
                    var a=item.peijian.split("=")
                    return (a[0])
                     
                }
            },
            {
                title:"配件细分",
                align:"center",
                render:(text,item,index)=>{
                    var a=item.peijian.split("=")
                    return (a[1])
                     
                }
            },
            {
                title:"出库时间",
                align:"center",
                render:(text)=>{
                    return moment(text.time).format("YYYY-MM-DD HH:mm:ss")
                }
            },
            {
                title:"出库数量",
                align:"center",
                render:(text,item)=>{
                    return (item.content)
                }
            },
            {
                title:"总价",
                align:"center",
                render:(text,item)=>{
                    return (item.dbmoney)
                }
            },
            {
                title:"审核是否通过",
                align:"center",
                render:(text)=>{
                    const {user} = myuser
                    console.log(user.role)
                    return (
                        <div>
                            {
                               text.isTest? <img src={require("../../assets/images/true.jpg").default} style={{width:80,height:80,margin:"auto"}}/> :<Switch onChange={(checked)=>this.changeIsTest(checked,text)}  disabled={user.role>=1?false:true}  size="small"  checked={text.isTest}></Switch>
                            }
                        </div>
                    )
                    
                }
            },
            {
                title:"操作",
                align:"center",
                render:(text,item,index)=>{
                    const {user} = myuser
                    return ( 
                        <div>
                            <Button onClick={()=>this.gotoPreview(item)} type="primary" size="middle" shape="circle" icon={<EyeOutlined />} ></Button>
                        </div>
                    )
                }
            }
        ]
    }

    componentDidMount(){
        chukuList.getChuku()
    }


    gotoPreview=(item)=>{
        history.push("/main/advise/preview?aid="+item._id)
    }

    changeIsTest=(checked,text)=>{
        console.log(text)
        ajax.updatesh({
            _id:text._id,
            isTest:checked,
            peijian:text.peijian,
            content:text.content,
        }).then(res=>{
            var list=chukuList.list.map(item=>{
                if(item._id==text._id){
                    item={...item,isTest:checked}
                }
                return item
            })
            chukuList.changeChuku(list)
        })
    }

    render() {
        return (
            <div>
                <h2 style={{textAlign:"center",color:"#f00",fontSize:30,fontWeight:600}}>出库表</h2>
                <Table
                    id="mytable"
                    style={{marginTop:15}}
                    dataSource={chukuList.list}
                    columns={this.state.columns}
                    bordered
                    pagination={{pageSize:10}}
                    rowKey={
                        (item)=>(item._id)
                    }
                ></Table>
            </div>
        );
    }
}

export default ChuList;